﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TBFramework;
using System;
public class PictureOptionsComponent : UIParentClass
{
    public Dropdown[] dropdown;
    public InputField[] inputs;
    public int[] indexs;
    public string[] values;
    public Image image;
    public Sprite[] sprites;

    public Sprite defaultSprite;
    Action<bool> action;

    Dictionary<string, string> formKeyValues;
    public void Start()
    {
        // Init();

    }
    public override void Init<T>(T _values, Action<bool> _action)
    {

        action = _action;
        formKeyValues = (_values as PictureOptions).formKeyValues;//键值对
        sprites = (_values as PictureOptions).t2d;
        defaultSprite = (_values as PictureOptions).d2d;
        image.sprite = defaultSprite;

        dropdown = new Dropdown[0];
        inputs = new InputField[0];
        indexs = new int[0];
        values = new string[0];

        dropdown = transform.GetComponentsInChildren<Dropdown>();
        inputs = transform.GetComponentsInChildren<InputField>();
        indexs = new int[dropdown.Length + inputs.Length];
        values = new string[dropdown.Length + inputs.Length];
        for (int i = 0; i < dropdown.Length; i++)
        {
            var i1 = i;
            dropdown[i].onValueChanged.RemoveAllListeners();
            indexs[i1] = 0;
            values[i1] = dropdown[i1].options[0].text;

            if (formKeyValues.ContainsKey(i1.ToString()))
            {
                formKeyValues[i1.ToString()] = values[i1];
            }
            else
            {
                formKeyValues.Add(i1.ToString(), values[i1]);
            }

            dropdown[i].onValueChanged.AddListener((int index) =>
            {
                indexs[i1] = index;
                values[i1] = dropdown[i1].options[index].text;
                if (formKeyValues.ContainsKey(i1.ToString()))
                {
                    formKeyValues[i1.ToString()] = values[i1];
                }
                else
                {
                    formKeyValues.Add(i1.ToString(), values[i1]);
                }
                ChangeImage();

            });
        }
        for (int i = 0; i < inputs.Length; i++)
        {
            var i1 = i + dropdown.Length;
            inputs[i].onValueChanged.RemoveAllListeners();

            if (formKeyValues.ContainsKey(i1.ToString()))
            {
                formKeyValues[i1.ToString()] = values[i1];
            }
            else
            {
                formKeyValues.Add(i1.ToString(), values[i1]);
            }
            inputs[i].onValueChanged.AddListener((string value) =>
            {
                indexs[i1] = i1;
                values[i1] = value;
                if (formKeyValues.ContainsKey(i1.ToString()))
                {
                    formKeyValues[i1.ToString()] = values[i1];
                }
                else
                {
                    formKeyValues.Add(i1.ToString(), values[i1]);
                }
                ChangeImage();

            });
        }
        if (formKeyValues != null)
        {
            foreach (KeyValuePair<string, string> pair in formKeyValues)
            {
                Debug.Log($"Key: {pair.Key}, Value: {pair.Value}");
            }
        }

    }
    public void ChangeImage()
    {
        string fileName = "";
        for (int i = 0; i < values.Length; i++)
        {
            fileName += values[i] + "_";
        }
        fileName = fileName.Substring(0, fileName.Length - 1);
        Sprite foundSprite = null;
        foreach (Sprite sprite in sprites)
        {
            if (sprite != null && sprite.name == fileName)
            {
                foundSprite = sprite;
                break;
            }
        }
        
        if (foundSprite != null)
        {
            image.sprite = foundSprite;
        }
        else
        {
            image.sprite = defaultSprite;
        }
    }
    public void ButtonClick()
    { 
        action?.Invoke(true);
    }

}
